home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / pctchnqs / 1992 / number1 / l7.asm < prev    next >
Assembly Source File  |  1992-01-24  |  3KB  |  63 lines

  1. ; C near-callable assembly function for inserting a new node in a
  2. ; linked list sorted by ascending order of the Value field. The list
  3. ; is circular; that is, it has a dummy node as both the head and the
  4. ; tail of the list. The dummy node is a sentinel, containing the
  5. ; largest possible Value field setting. Tested with TASM 3.0.
  6.  
  7. MAX_TEXT_LENGTH equ 100         ;longest allowed Text field
  8. SENTINEL equ  32767             ;largest possible Value field
  9.  
  10. LinkNode struc
  11. NextNode dw     ?
  12. Value   dw      ?
  13. Text    db      MAX_TEXT_LENGTH+1 dup(?)
  14. ;*** Any number of additional data fields may by present ***
  15. LinkNode ends
  16.  
  17.         .model  small
  18.         .code
  19.  
  20. ; Inserts the specified node into a ascending-value-sorted linked
  21. ; list, such that value-sorting is maintained. Returns a pointer to
  22. ; the node after which the new node is inserted.
  23. ; C near-callable as:
  24. ; struct LinkNode *InsertNodeSorted(struct LinkNode *HeadOfListNode,
  25. ;      struct LinkNode *NodeToInsert)
  26.  
  27. parms   struc
  28.         dw      2 dup (?)       ;pushed return address & BP
  29. HeadOfListNode dw       ?       ;pointer to head node of list
  30. NodeToInsert dw         ?       ;pointer to node to insert
  31. parms   ends
  32.  
  33.         public  _InsertNodeSorted
  34. _InsertNodeSorted proc  near
  35.         push    bp
  36.         mov     bp,sp                   ;point to stack frame
  37.         push    si                      ;preserve register vars
  38.         push    di
  39.         mov     si,[bp].NodeToInsert    ;point to node to insert
  40.         mov     ax,[si].Value           ;search value
  41.         mov     di,[bp].HeadOfListNode  ;point to linked list in
  42.                                         ; which to insert
  43. SearchLoop:
  44.         mov     bx,di                   ;advance to the next node
  45.         mov     di,[bx].NextNode        ;point to following node
  46.         cmp     [di].Value,ax           ;is the following node's
  47.                                         ; value less than the value
  48.                                         ; from the node to insert?
  49.         jl      SearchLoop              ;yes, so continue searching
  50.                                         ;no, so we have found our
  51.                                         ; insert point
  52.         mov     ax,[bx].NextNode        ;link the new node between
  53.         mov     [si].NextNode,ax        ; the current node and the
  54.         mov     [bx].NextNode,si        ; following node
  55.         mov     ax,bx                   ;return pointer to node
  56.                                         ; after which we inserted
  57.         pop     di                      ;restore register vars
  58.         pop     si
  59.         pop     bp
  60.         ret
  61. _InsertNodeSorted endp
  62.         end
  63.